<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form onsubmit="return false">
        <p><label for="">用户名：<input type="text" name="user" class="user"></label></p>
        <p><label for="">密&nbsp;&nbsp;码：<input type="text" name="pwd" class="pwd"></label></p>
        <button class="btn">提交</button>
    </form>
    <script>
        const user = document.querySelector(".user");
        const pwd = document.querySelector(".pwd");
        const btn = document.querySelector(".btn");
        btn.onclick = function () {
            var userIpt = user.value;
            var pwdIpt = pwd.value;
            if (userIpt && pwdIpt) {
                var xhr = new XMLHttpRequest();
                xhr.open("post", "../5.post.php", true);
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xhr.send("user=" + userIpt + "&pwd=" + pwdIpt);
                xhr.onreadystatechange = function () {
                    if (xhr.readystate == 4 && xhr.status == 200) {
                        var date = JSON.parse(xhr.responseText);
                        console.log(data);
                    }
                }
            }
        }
    </script>
</body>

</html>